﻿using Dapper;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OMG.Fill.Mulberry.Model;
using OMG.Fill.Mulberry.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace OMG.Fill.Mulberry.Service
{
    /// <summary>
    /// 桑蚕种场生产经营情况调查表通用操作类
    /// </summary>
    public class MulSilkwormFields
    {
        ReportServices rs = new ReportServices();
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(MulSilkwormFieldEntity model) {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.MulSilkwormField";
            strSql +="(";
            strSql +="ID,";
            strSql +="ReportId,";
            strSql +="PersonalTotal,";
            strSql +="KJRYNum,";
            strSql +="GJZCNum,";
            strSql +="SCYFMJ,";
            strSql +="ZYSYMJ,";
            strSql +="SCZCL_YZ,";
            strSql +="SCZCL_ZJ,";
            strSql +="CZXS_YZ,";
            strSql +="CZXS_ZJ,";
            strSql +="FixedAssets,";
            strSql +="MulSilkwormProduct,";
            strSql +="Cost,";
            strSql +="Profit,";
            strSql +="Remark)";
            strSql +=" values(";
            strSql +="@ID,";
            strSql +="@ReportId,";
            strSql +="@PersonalTotal,";
            strSql +="@KJRYNum,";
            strSql +="@GJZCNum,";
            strSql +="@SCYFMJ,";
            strSql +="@ZYSYMJ,";
            strSql +="@SCZCL_YZ,";
            strSql +="@SCZCL_ZJ,";
            strSql +="@CZXS_YZ,";
            strSql +="@CZXS_ZJ,";
            strSql +="@FixedAssets,";
            strSql +="@MulSilkwormProduct,";
            strSql +="@Cost,";
            strSql +="@Profit,";
            strSql +="@Remark)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    model.ID= CustomFun.GetGuid();
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "添加成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "添加失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增桑蚕种场生产经营情况调查表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(MulSilkwormFieldEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"UPDATE dbo.MulSilkwormField set ";
            strSql+="ReportId = @ReportId,";
            strSql+="PersonalTotal = @PersonalTotal,";
            strSql+="KJRYNum = @KJRYNum,";
            strSql+="GJZCNum = @GJZCNum,";
            strSql+="SCYFMJ = @SCYFMJ,";
            strSql+="ZYSYMJ = @ZYSYMJ,";
            strSql+="SCZCL_YZ = @SCZCL_YZ,";
            strSql+="SCZCL_ZJ = @SCZCL_ZJ,";
            strSql+="CZXS_YZ = @CZXS_YZ,";
            strSql+="CZXS_ZJ = @CZXS_ZJ,";
            strSql+="FixedAssets = @FixedAssets,";
            strSql+="MulSilkwormProduct = @MulSilkwormProduct,";
            strSql+="Cost = @Cost,";
            strSql+="Profit = @Profit,";
            strSql+="Remark = @Remark";
            strSql+=" where ID = @ID";
            using (var dbCon = ConnectFactory.GetOpenConnection())
            {
                var trans = dbCon.BeginTransaction();
                try
                {
                    int result = dbCon.Execute(strSql, model, trans);
                    string strRep = "update dbo.T_Report set Status='2',UpdateTime=@UpdateTime where Id=@ReportId";
                    dbCon.Execute(strRep, new { ReportId = model.ReportId, UpdateTime = DateTime.Now }, trans);
                    //提交事务
                    trans.Commit();
                    obj.model = result;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "更新成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
                catch (Exception ex)
                {
                    //出现异常，事务Rollback
                    trans.Rollback();
                    obj.status = DefaultKey.ErrorResult;
                    obj.message = ex.ToString().Trim();
                    LogHelper.ErrorLog("更新桑蚕种场生产经营情况调查表对象信息失败", ex);
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.MulSilkwormField where ID = @ID";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { ID = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除桑蚕种场生产经营情况调查表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.MulSilkwormField where ID in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除桑蚕种场生产经营情况调查表表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        //public virtual ResResult<MulSilkwormFieldEntity> GetModel(string id) {
        //    ResResult<MulSilkwormFieldEntity> obj = new ResResult<MulSilkwormFieldEntity>();
        //    string strSql = @"select * from dbo.MulSilkwormField where ID = @ID";
        //    try
        //    {
        //        using (var dbCon = ConnectFactory.GetOpenConnection())
        //        {
        //            obj.model = dbCon.Query<MulSilkwormFieldEntity>(strSql, new { ID = id }).FirstOrDefault();
        //            obj.status = DefaultKey.SuccessResult;
        //            obj.message = "获取成功！";
        //            dbCon.Dispose();//数据库连接释放
        //            return obj;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        obj.status = DefaultKey.ErrorResult;
        //        obj.message = ex.ToString().Trim();
        //        LogHelper.ErrorLog("获取桑蚕种场生产经营情况调查表对象信息失败", ex);
        //        return obj;
        //    }
        //}
        public  ResResult<MulSilkwormFieldEntity> GetModel(string ReportId)
        {
            ResResult<MulSilkwormFieldEntity> obj = new ResResult<MulSilkwormFieldEntity>();
            MulSilkwormFieldEntity model = new MulSilkwormFieldEntity();
            if (CheckRepCount(ReportId))
            {
                model.ReportId = ReportId;
                if (AddInfo(model).status > 0)
                {
                    obj.model = model;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                }
            }
            else
            {
                string strSql = @"select * from dbo.MulSilkwormField where ReportId = @ReportId";
                try
                {
                    using (var dbCon = ConnectFactory.GetOpenConnection())
                    {
                        obj.model = dbCon.Query<MulSilkwormFieldEntity>(strSql, new { ReportId = ReportId }).FirstOrDefault();
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "获取成功！";
                        dbCon.Dispose();//数据库连接释放
                    }
                }
                catch (Exception ex)
                {
                    obj.status = DefaultKey.ErrorResult;
                    obj.message = ex.ToString().Trim();
                    LogHelper.ErrorLog("获取桑蚕种场生产经营情况调查表对象信息失败", ex);

                }
            }
            return obj;
        }
        /// <summary>
        /// 判定是否生成过报表
        /// </summary>
        /// <param name="reportId"></param>
        /// <returns>没生成过true,否则false</returns>
        public bool CheckRepCount(string reportId)
        {
            string strSql = @"select count(1) from dbo.MulSilkwormField where ReportId=@ReportId";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    //为0，没生成过true,否则false
                    return dbCon.Query<int>(strSql, new { ReportId = reportId }).FirstOrDefault() == 0 ? true : false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("判断是否生成过表桑蚕种场生产经营情况调查表MulSilkwormField失败", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取今往年数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResResult<MulSilkwormFieldViewModel> GetModel(MulNowModel model)
        {
            ResResult<MulSilkwormFieldViewModel> obj = new ResResult<MulSilkwormFieldViewModel>();
            MulSilkwormFieldEntity vfEntity = new MulSilkwormFieldEntity();
            MulSilkwormFieldViewModel vvm = new MulSilkwormFieldViewModel();
            DictServices dictServices = new DictServices();
            var paramlist = new DynamicParameters();
            string strSqlReport = "";
            if (model != null)
            {
                if (!string.IsNullOrEmpty(model.AreaCode) && !string.IsNullOrEmpty(model.ReportCode) && !string.IsNullOrEmpty(model.FillInYear))
                {
                    string FillInYear = dictServices.GetKey(model.FillInYear);
                    paramlist.Add("AreaCode", model.AreaCode);
                    paramlist.Add("ReportCode", model.ReportCode);
                    paramlist.Add("FillInYear", FillInYear);
                    int OldFillInYear = Convert.ToInt32(FillInYear) - 1;
                    if (OldFillInYear < 0)
                    {
                        OldFillInYear = 0;
                    }
                    paramlist.Add("OldFillInYear", OldFillInYear.ToString());
                    string strNowSql = "SELECT  Status ReportStatus,ReportCode,MulSilkwormField.ID,ReportId ,PersonalTotal ,KJRYNum,GJZCNum  ";
                    strNowSql += ",SCYFMJ,ZYSYMJ,SCZCL_YZ,SCZCL_ZJ,CZXS_YZ,CZXS_ZJ,FixedAssets  ";
                    strNowSql += ",MulSilkwormProduct,Cost,Profit,Remark   ";
                    strNowSql += "FROM T_Report r left join   dbo.MulSilkwormField MulSilkwormField   ";
                    strNowSql += "ON r.Id = MulSilkwormField.ReportId  ";
                    strNowSql += "WHERE ReportCode = @ReportCode and FillInYear = @FillInYear and AreaCode = @AreaCode ";
                    string strOldSql = "SELECT Status OldReportStatus, ReportCode,MulSilkwormField.ID OldID,ReportId OldReportId ,PersonalTotal  OldPersonalTotal,KJRYNum OldKJRYNum,GJZCNum OldGJZCNum";
                    strOldSql += ",SCYFMJ OldSCYFMJ,ZYSYMJ OldZYSYMJ,SCZCL_YZ OldSCZCL_YZ,SCZCL_ZJ OldSCZCL_ZJ,CZXS_YZ OldCZXS_YZ,CZXS_ZJ OldCZXS_ZJ,FixedAssets OldFixedAssets";
                    strOldSql += ",MulSilkwormProduct OldMulSilkwormProduct,Cost OldCost,Profit OldProfit,Remark OldRemark   ";
                    strOldSql += "FROM T_Report r left join   dbo.MulSilkwormField MulSilkwormField   ";
                    strOldSql += "ON r.Id = MulSilkwormField.ReportId  ";
                    strOldSql += "WHERE ReportCode = @ReportCode and FillInYear = @FillInYear and AreaCode = @AreaCode ";
                    strSqlReport = "SELECT * FROM ((" + strNowSql + ")t left JOIN  (" + strOldSql + ")tt ON t.ReportCode = tt.ReportCode)";

                    Boolean currentResult = rs.CheckRepCreate(model.ReportCode, model.AreaCode, FillInYear);
                    Boolean OldResult = rs.CheckRepCreate(model.ReportCode, model.AreaCode, OldFillInYear.ToString());
                    //判断选择的年度与上一年度是否生成过报表
                    if (currentResult && OldResult)
                    {
                        obj.model = vvm;
                        obj.status = -1;
                        obj.message = model.FillInYear + "与上一年的报表没有填报数据！！";
                    }
                    //判断本年是否生成过报表
                    else if (currentResult)
                    {
                        var dbCon = ConnectFactory.GetOpenConnection();
                        obj.model = dbCon.Query<MulSilkwormFieldViewModel>(strOldSql, paramlist).FirstOrDefault();
                        obj.status = -2;
                        obj.message = model.FillInYear + "的报表没有填报数据！！";
                        dbCon.Dispose();//数据库连接释放
                    }
                    else
                    {
                        var dbCon = ConnectFactory.GetOpenConnection();
                        //获取报表Id
                        vfEntity.ReportId = rs.GetReportId(model.ReportCode, model.AreaCode, FillInYear);
                        //判断表T_VegFacility是否初始化过
                        if (CheckRepCount(vfEntity.ReportId))
                        {
                            if (AddInfo(vfEntity).status > 0)//报表初始化
                            {
                                //查询去年数据
                                obj.model = dbCon.Query<MulSilkwormFieldViewModel>(strOldSql, paramlist).FirstOrDefault();
                                obj.status = 1;//初始化成功status为1
                                obj.message = "MulSilkwormField报表初始化成功！";
                                return obj;
                            }
                        }
                        else
                        {
                            try
                            {
                                obj.model = dbCon.Query<MulSilkwormFieldViewModel>(strSqlReport, paramlist).FirstOrDefault();
                                obj.status = 2;//成功状态为2
                                obj.message = "获取MulSilkwormField报表今往年数据成功！";
                                return obj;
                            }
                            catch (Exception ex)
                            {
                                obj.status = 0; //失败状态
                                obj.message = ex.ToString().Trim();
                                LogHelper.ErrorLog("获取MulSilkwormField报表今往年数据失败", ex);
                            }
                        }
                    }
                }
            }
            return obj;
        }


        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<MulSilkwormFieldEntity>> GetList()
        {
            ResResult<List<MulSilkwormFieldEntity>> obj = new ResResult<List<MulSilkwormFieldEntity>>();
            string strSql = @"select * from dbo.MulSilkwormField";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<MulSilkwormFieldEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新桑蚕种场生产经营情况调查表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 读取导入的Excel数据
        /// </summary>
        /// <param name="pathName">导入文件路径</param>
        /// <param name="fileSuffix">文件后缀</param>
        /// <param name="sheetName">sheet名称</param>
        /// <param name="startRow">从第几行开始读取数据</param>
        /// <returns></returns>
        public List<MulSilkwormFieldInfoModel> ExcelToModel(string pathName, string fileSuffix, string sheetName, int startRow)
        {


            IWorkbook workbook = null;
            ISheet sheet = null;
            List<MulSilkwormFieldInfoModel> list = new List<MulSilkwormFieldInfoModel>();
            FileStream fs = new FileStream(pathName, FileMode.Open, FileAccess.Read);
            switch (fileSuffix)
            {
                case ".xls":
                    workbook = new HSSFWorkbook(fs);
                    break;
                case ".xlsx":
                    workbook = new XSSFWorkbook(fs);
                    break;
                default:
                    workbook = new HSSFWorkbook(fs);
                    break;
            }
            if (!string.IsNullOrEmpty(sheetName))
            {
                sheet = workbook.GetSheet(sheetName);
                if (sheet == null) //如果没有找到指定的sheetName对应的sheet，则尝试获取第一个sheet
                {
                    sheet = workbook.GetSheetAt(0);
                }
            }
            else
            {
                sheet = workbook.GetSheetAt(0);
            }
            if (sheet != null)
            {
                //获取有数据的行数
                int rowCount = 0;
                for (int i = startRow; i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    if (row != null)
                    {
                        rowCount = sheet.GetRow(i).RowNum;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int i = startRow; i <= rowCount; i++)
                {
                    IRow row = sheet.GetRow(i);
                    MulSilkwormFieldInfoModel model = new MulSilkwormFieldInfoModel();
                    if (row.GetCell(0) != null && row.GetCell(0).ToString() != "")
                    {
                        model.PersonalTotal = Convert.ToInt32(row.GetCell(0).ToString());
                    }
                    else
                    {
                        model.PersonalTotal = null;
                    }
                    if (row.GetCell(1) != null && row.GetCell(1).ToString() != "")
                    {
                        model.KJRYNum = Convert.ToInt32(row.GetCell(1).ToString());
                    }
                    else
                    {
                        model.KJRYNum = null;
                    }
                    if (row.GetCell(2) != null && row.GetCell(2).ToString() != "")
                    {
                        model.GJZCNum = Convert.ToInt32(row.GetCell(2).ToString());
                    }
                    else
                    {
                        model.GJZCNum = null;
                    }
                    if (row.GetCell(3) != null && row.GetCell(3).ToString() != "")
                    {
                        model.SCYFMJ = decimal.Parse(row.GetCell(3).ToString());
                    }
                    else
                    {
                        model.SCYFMJ = null;
                    }
                    if (row.GetCell(4) != null && row.GetCell(4).ToString() != "")
                    {
                        model.ZYSYMJ = decimal.Parse(row.GetCell(4).ToString());
                    }
                    else
                    {
                        model.ZYSYMJ = null;
                    }
                    if (row.GetCell(5) != null && row.GetCell(5).ToString() != "")
                    {
                        model.SCZCL_YZ = decimal.Parse(row.GetCell(5).ToString());
                    }
                    else
                    {
                        model.SCZCL_YZ = null;
                    }
                    if (row.GetCell(6) != null && row.GetCell(6).ToString() != "")
                    {
                        model.SCZCL_ZJ = decimal.Parse(row.GetCell(6).ToString());
                    }
                    else
                    {
                        model.SCZCL_ZJ = null;
                    }
                    if (row.GetCell(7) != null && row.GetCell(7).ToString() != "")
                    {
                        model.CZXS_YZ = decimal.Parse(row.GetCell(7).ToString());
                    }
                    else
                    {
                        model.CZXS_YZ = null;
                    }
                    if (row.GetCell(8) != null && row.GetCell(8).ToString() != "")
                    {
                        model.CZXS_ZJ = Convert.ToDecimal(row.GetCell(8).ToString());
                    }
                    else
                    {
                        model.CZXS_ZJ = null;
                    }
                    if (row.GetCell(9) != null && row.GetCell(9).ToString() != "")
                    {
                        model.FixedAssets = Convert.ToDecimal(row.GetCell(9).ToString());
                    }
                    else
                    {
                        model.FixedAssets = null;
                    }
                    if (row.GetCell(10) != null && row.GetCell(10).ToString() != "")
                    {
                        model.MulSilkwormProduct = Convert.ToDecimal(row.GetCell(10).ToString());
                    }
                    else
                    {
                        model.MulSilkwormProduct = null;
                    }
                    if (row.GetCell(11) != null && row.GetCell(11).ToString() != "")
                    {
                        model.Cost = Convert.ToDecimal(row.GetCell(11).ToString());
                    }
                    else
                    {
                        model.Cost = null;
                    }
                    if (row.GetCell(12) != null && row.GetCell(12).ToString() != "")
                    {
                        model.Profit = Convert.ToDecimal(row.GetCell(12).ToString());
                    }
                    else
                    {
                        model.Profit = null;
                    }
                    if (row.GetCell(13) != null && row.GetCell(13).ToString() != "")
                    {
                        model.Remark = row.GetCell(13).ToString();
                    }
                    else
                    {
                        model.Remark = null;
                    }
                    list.Add(model);
                }
            }
            return list;
        }
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(MulSilkwormFieldInfoModel model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.MulSilkwormField";
            strSql += "(";
            strSql += "Id";
            strSql += ",ReportId";
            strSql += ",PersonalTotal";
            strSql += ",KJRYNum";
            strSql += ",GJZCNum";
            strSql += ",SCYFMJ";
            strSql += ",ZYSYMJ";
            strSql += ",SCZCL_YZ";
            strSql += ",SCZCL_ZJ";
            strSql += ",CZXS_YZ";
            strSql += ",CZXS_ZJ";
            strSql += ",FixedAssets";
            strSql += ",MulSilkwormProduct";
            strSql += ",Cost";
            strSql += ",Profit";
            strSql += ",Remark)";
            strSql += " values(";
            strSql += "@Id";
            strSql += ",@ReportId";
            strSql += ",@PersonalTotal";
            strSql += ",@KJRYNum";
            strSql += ",@GJZCNum";
            strSql += ",@SCYFMJ";
            strSql += ",@ZYSYMJ";
            strSql += ",@SCZCL_YZ";
            strSql += ",@SCZCL_ZJ";
            strSql += ",@CZXS_YZ";
            strSql += ",@CZXS_ZJ";
            strSql += ",@FixedAssets";
            strSql += ",@MulSilkwormProduct";
            strSql += ",@Cost";
            strSql += ",@Profit";
            strSql += ",@Remark)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    model.ID = CustomFun.GetGuid();
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "添加成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "添加失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增表12桑蚕种场生产经营情况调查表MulSilkwormFieldy对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(MulSilkwormFieldInfoModel model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"UPDATE dbo.MulSilkwormField set ";
            strSql += "PersonalTotal=@PersonalTotal";
            strSql += ",KJRYNum=@KJRYNum";
            strSql += ",GJZCNum=@GJZCNum";
            strSql += ",SCYFMJ=@SCYFMJ";
            strSql += ",ZYSYMJ=@ZYSYMJ";
            strSql += ",SCZCL_YZ=@SCZCL_YZ";
            strSql += ",SCZCL_ZJ=@SCZCL_ZJ";
            strSql += ",CZXS_YZ=@CZXS_YZ";
            strSql += ",CZXS_ZJ=@CZXS_ZJ";
            strSql += ",FixedAssets=@FixedAssets";
            strSql += ",MulSilkwormProduct=@MulSilkwormProduct";
            strSql += ",Cost=@Cost";
            strSql += ",Profit=@Profit";
            strSql += ",Remark=@Remark   ";
            strSql += " where ReportId = @ReportId";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "更新成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "更新失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新表12桑蚕种场生产经营情况调查表MulSilkwormFieldy对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public ResResult<int> DeleteData(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.MulSilkwormField where ReportId = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除表12桑蚕种场生产经营情况调查表MulSilkwormFieldy对象信息失败", ex);
                return obj;
            }
        }
    }
}